﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Xml;
using Sherwood.Web;

namespace Sherwood.Content.SearchServices
{
    public interface ISolrXmlInjector<in TRecord> where TRecord : IRecord
    {
        /// <summary>
        /// Is called before executing the solr query. Notice that the injectors are reused,
        /// but a injector will never be used by multiple threads at the same time.
        /// </summary>
        /// <param name="queryString">Read from or alter the solr query string</param>
        void Prepare(QueryParameterCollection queryString);

        /// <summary>
        /// Is called for every X record and is used to bulk prefetch data needed for injection.
        /// The value returned will be provided on the <see cref="Inject"/> method.
        /// </summary>
        /// <param name="recordIds">Ids of records to prefetch data for</param>
        /// <param name="getRecords">A handle to get the records, if needed</param>
        /// <param name="needsRecordsOnInjection">Set to true if the record object is needed on injection</param>
        /// <returns>An object which will be provided on injection</returns>
        object Prefetch(IEnumerable<Guid> recordIds, Func<IEnumerable<TRecord>> getRecords, out bool needsRecordsOnInjection);

        /// <summary>
        /// Inject xml data into the record xml returned from solr.
        /// </summary>
        /// <param name="solrValues">The original values returned from solr</param>
        /// <param name="recordId">The id of the record we are injecting data for</param>
        /// <param name="record">The record object. Is null if needsRecordsOnInjection was set to false on Prefetch</param>
        /// <param name="prefetchValues">The value returned by the Prefetch method</param>
        /// <param name="writer">The writer used to writer xml data</param>
        void Inject(NameValueCollection solrValues, Guid recordId, TRecord record, object prefetchValues, XmlWriter writer);
    }
}